*===============================================================================
*=== FILE 4 --- CREATE CLIMATE CHANGE INDEX BY QUARTER AND YEAR
*===============================================================================
clear
set more off
 
cap cd ""


foreach suffix in "" _FR {

	// NC NJ SC removed due to insufficient observations in replication sample 
	foreach state in FL { 

		/* Generate index time series for the entire state at yearly frequency*/

		use "replication output/`state'_masterfile`suffix'", clear
		gen observation = 1

		collapse (sum) has_index observation, by (listing_year)
		gen hit_share = has_index/observation
		save "replication output/`state'_state_index_yearly`suffix'", replace

		clear


		/* Generate index time series for the entire state at quarterly frequency*/
		use "replication output/`state'_masterfile`suffix'", clear
		gen observation = 1

		collapse (sum) has_index observation, by (listing_quarter)
		gen hit_share = has_index/observation
		save "replication output/`state'_state_index_quarterly`suffix'", replace

		clear


		/* Generate index time series by zipcode at yearly frequency*/
		use "replication output/`state'_masterfile`suffix'", clear
		gen observation = 1

		collapse (sum) has_index observation, by (postalcode_int listing_year)
		gen hit_share = has_index/observation
		save "replication output/`state'_zipcode_index_yearly`suffix'", replace

		clear


		/* Generate index time series by zipcode at quarterly frequency*/
		use "replication output/`state'_masterfile`suffix'", clear
		gen observation = 1

		collapse (sum) has_index observation, by (postalcode_int listing_quarter)
		gen hit_share = has_index/observation
		save "replication output/`state'_zipcode_index_quarterly`suffix'", replace

		
		/* Generate index time series by zipcode for full horizon*/
		use "replication output/`state'_masterfile`suffix'", clear
		gen observation = 1
		keep if listing_year >= 2008

		collapse (sum) has_index observation, by (postalcode_int)
		gen hit_share = has_index/observation
		
		rename has_index has_hit
		save "replication output/`state'_zipcode_index`suffix'", replace
		
	}


*===============================================================================
*=== Now merge this back onto the Master File
*===============================================================================
	// NC NJ SC removed due to insufficient observations in replication sample 
	foreach state in FL  {

		use "replication output/`state'_masterfile`suffix'", clear

		merge m:1 listing_year using "replication output/`state'_state_index_yearly`suffix'.dta"
		keep if _merge == 3
		rename hit_share index_state_year
		drop _merge observation 


		merge m:1 listing_quarter using "replication output/`state'_state_index_quarterly`suffix'.dta"
		keep if _merge == 3
		rename hit_share index_state_quarter
		drop _merge observation 


		merge m:1 postalcode_int listing_year using "replication output/`state'_zipcode_index_yearly`suffix'.dta"
		keep if _merge == 3
		rename hit_share index_zipcode_year
		drop _merge observation 


		merge m:1 postalcode_int listing_quarter using "replication output/`state'_zipcode_index_quarterly`suffix'.dta"
		keep if _merge == 3
		rename hit_share index_zipcode_quarter
		drop _merge observation 

		save "replication output/`state'_masterfile`suffix'", replace
		clear

	}


*===============================================================================
*=== Make master file with all states
*===============================================================================
	* again, NC, SC and NJ omitted in the replication sample
	use "replication output/FL_masterfile`suffix'", replace
	* append using "replication output/SC_masterfile`suffix'"

	save "replication output/_FS_FourStates_Masterfile`suffix'", replace
}
